/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "crcore.h"

static pthread_once_t exception_environment_once = PTHREAD_ONCE_INIT;
static pthread_key_t exception_environment_key;

const result_code MEMORY_ERROR = {EC(1),"Failed to allocate memory"};
const result_code SOCKET_ERROR = {EC(2),"Socket closed"};
const result_code SOCKET_TIMEOUT = {EC(3),"Socket operation timed out"};

//this will be called when the thread is destroyed
static void exception_environment_dtor(void* arg){
	exception_env_t* env = (exception_env_t*)arg;
	exception_env_t* prev;
	while(env){
		prev = env->previous_env;
		free(env);
		env = prev;
	}
}

static void create_exception_environment_key(void){
	pthread_key_create(&exception_environment_key,exception_environment_dtor);
}

void init_exception_environment(void)
{
	pthread_once(&exception_environment_once,create_exception_environment_key);
	pthread_setspecific(exception_environment_key,NULL);
}

void push_exception_environment(exception_env_t* env){
	pthread_once(&exception_environment_once,create_exception_environment_key);
	env->previous_env = (exception_env_t*) pthread_getspecific(exception_environment_key);
	pthread_setspecific(exception_environment_key,(void*)env);
}

exception_env_t* get_exception_environment(void){
	exception_env_t* env = (exception_env_t*) pthread_getspecific(exception_environment_key);
	return env;
}

exception_env_t* pop_exception_environment(void){
	exception_env_t* env = (exception_env_t*) pthread_getspecific(exception_environment_key);
	if(env)
	{
		pthread_setspecific(exception_environment_key,(void*)env->previous_env);
		env->previous_env=NULL;
	}
	else
		pthread_setspecific(exception_environment_key,NULL);
	return env;
}

void cleanup_exception_environment(exception_env_t* env, int do_cleanup)
{
	while(env->garbage_list.head.next)
	{
		garbage_list_node* node = (garbage_list_node*) env->garbage_list.head.next;
		if(do_cleanup)
		{
			node->cleanup_func(node);
		}
		list_remove(&env->garbage_list,&node->node);
		free(node);
	}
}
void remove_garbage_node(garbage_list_node* node)
{
	if(!node)
		return;
	exception_env_t* env = get_exception_environment();
	if(env)
		list_remove(&env->garbage_list,&node->node);
	free(node);
}
garbage_list_node* add_garbage_node(void* data, void(*cleanup_func)(struct garbage_list_node*))
{
	exception_env_t* env = get_exception_environment();
	if(env)
	{
		garbage_list_node* n = (garbage_list_node*) malloc(sizeof(garbage_list_node));
		memset(n,0,sizeof(garbage_list_node));
		n->data = data;
		n->cleanup_func = cleanup_func;
		list_add_tail(&env->garbage_list,&n->node);
		return n;
	}
	return NULL;
}

void print_exception(const char* prefix,const exception_env_t* e)
{
	if(prefix)
		printf("%s:",prefix);
	if(e->code)
		printf("try %s throw %s %s:%s\n",e->try_func_name,e->throw_func_name,e->code->information_string,e->extended_information_string);
	else
		printf("%s\n",e->extended_information_string);
}
